<?php
$customers_obj = new Customer();

function show_all(){
	global $customers_obj, $KToolBar;
	$query = "SELECT * FROM `".$customers_obj->get_table_name()."` ORDER BY `id` DESC";
	$paginate = paginate($customers_obj, $query, $_GET['pg_no'], $_GET['limit']);
	$customers = $paginate['obj'];
//	$customers = $customers_obj->select_row_query("SELECT * FROM `".$customers_obj->get_table_name()."` ORDER BY `id` DESC LIMIT 0, 30", "");
	$cust_category = "All Customers";
	
	include_once 'views/customers.html.php';
}

function show_managers(){
	global $customers_obj, $KToolBar;
	$customers = $customers_obj->find(array("user_type"=>"manager"));
	$cust_category = "Managers";
	
	include_once 'views/customers.html.php';
} 

function show_manager_details(){
	global $customers_obj;
	$id = $_GET['id'];
	$manager = $customers_obj->find((int)$id);
	$units = new Unit();
	$units = $units->find(array("owner_id"=>$id));
	
	include_once 'views/manager.html.php';
}

function show_guests(){
	global $customers_obj, $KToolBar;
	$customers = $customers_obj->find(array("user_type"=>"guest"));
	$cust_category = "Guests";
	
	include_once 'views/customers.html.php';
}

function new_customer(){
	global $KToolBar;
	include_once 'views/customers_create_or_edit.html.php';
}

function edit_customer(){
	global $customers_obj, $KToolBar;
	$cid = $_GET['cid'];
	$customer = $customers_obj->find((int)$cid);
	
	include_once 'views/customers_create_or_edit.html.php';
}

function save_customer(){
	global $customers_obj, $database, $notice, $mosConfig_dbprefix;
	$notice['error'] = 1;
	if(!filter_var($_POST['customer']['email'], FILTER_VALIDATE_EMAIL)){
		$notice['msg'] = "Please enter a valid Email address";
	}
	else{
		if($_POST['customer']['user_type']){
			$_POST['customer']['user_type'] = "manager";
			$manager_name = $_POST['customer']['title']." ".$_POST['customer']['firstname']." ".$_POST['customer']['surname'];
			// check what is GID in the `jos_users` table
			//$db->setQuery("INSERT INTO `#__users` VALUES(null, '$manager_name', '".$_POST['customer']['username']."', '".$_POST['customer']['email']."', '".md5($_POST['customer']['password'])."', 'Registered', 0, 0, '18', '".date("Y-m-d H:i:s")."', null, null, null)");
//			$db->query();
			$query = "INSERT INTO `".$mosConfig_dbprefix."users` VALUES(null, '$manager_name', '".$_POST['customer']['username']."', '".$_POST['customer']['email']."', '".md5($_POST['customer']['password'])."', 'Registered', 0, 0, '18', '".date("Y-m-d H:i:s")."', '".date("Y-m-d H:i:s")."', '', '')";
			mysql_query($query) or die(mysql_error());
			$_POST['customer']['joomla_id'] = mysql_insert_id();
		}
		$notice = $customers_obj->insert_or_update($_POST['customer'], "");
	}
}

function search_customers(){
	global $customers_obj, $KToolBar;
	$cust_category = "All customers";
	$query = trim($_POST['q']);
	$names = explode(" ", $query);
	$limit = $_POST['limit'] ? $_POST['limit'] : 30;
	if(count($names) > 1){
		$query = "SELECT * FROM `".$customers_obj->get_table_name()."` WHERE `firstname` LIKE '%".$names[0]."%' AND `surname` LIKE '%".$names[1]."%' ORDER BY `id` DESC";
		$paginate = paginate($customers_obj, $query, $_GET['pg_no'], $limit);
		$customers = $paginate['obj'];
//		$customers = $customers_obj->select_row_query("SELECT * FROM `".$customers_obj->get_table_name()."` WHERE `firstname` LIKE '%".$names[0]."%' AND `surname` LIKE '%".$names[1]."%' ORDER BY `id` DESC LIMIT 0, $limit", "");
	}
	else {
		$query = "SELECT * FROM `".$customers_obj->get_table_name()."` WHERE `firstname` LIKE '%".$query."%' OR `surname` LIKE '%".$query."%' ORDER BY `id` DESC";
		$paginate = paginate($customers_obj, $query, $_GET['pg_no'], $limit);
		$customers = $paginate['obj'];
//		$customers = $customers_obj->select_row_query("SELECT * FROM `".$customers_obj->get_table_name()."` WHERE `firstname` LIKE '%".$query."%' OR `surname` LIKE '%".$query."%' ORDER BY `id` DESC LIMIT 0, $limit", "");
	}
	include_once 'views/customers.html.php';
}

function delete_selected_customers(){
	global $customers_obj;
	$msg = delete_selected_items($_POST['customers'], $customers_obj);
}
?>
